const koa = require('koa')
const app = new koa()
const mongoose = require('mongoose')
const {connect, initSchema} = require('./Schema/init.js')
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')
const cors = require('koa2-cors')
const static = require('koa-static')
const path = require('path')

app.use(bodyParser())
app.use(cors())

;(async () =>{
    await connect()
    initSchema()
})()

let router = new Router()

// 装载所有子路由
let user = require('./routers/user.js')
let home = require('./routers/home.js')
let goods = require('./routers/goods.js')

router.use('/user', user.routes())
router.use('/home', home.routes())
router.use('/goods', goods.routes())

const staticPath = './static'
app.use(static(
    path.join( __dirname, staticPath)
))
// 加载路由中间件
app.use(router.routes())
app.use(router.allowedMethods())


app.listen(3000, ()=>{
    console.log('[server] starting at 3000')
})
